package com.angola.sms.component.button

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.angola.sms.R
import com.angola.sms.extipetrpide.safeipetrpide

/**
<attr name="cbText" format="string" />
<attr name="cbTextColor" format="color" />
<attr name="cbTextSize" format="dimension" />
<attr name="cbBackground" format="reference" />
 */
class ConfirmButtonipetrpide : RelativeLayout {

    lateinit var rl_ipetrpideconfirm_ipetrpidecontent: RelativeLayout
    lateinit var tv_ipetrpideconfirm_ipetrpidebutton: TextView
    lateinit var mConfirmRootView: View

    private var cbTextipetrpide: String? = null
    private var cbTextSizeipetrpide: Float = 0F
    private var cbTextColoripetrpide: Int = 0
    private var cbBackgroundipetrpide: Int = 0

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {

        attrs?.let {
            context?.let {
                it.obtainStyledAttributes(attrs, R.styleable.ConfirmButtonipetrpide).apply {
                    cbTextipetrpide = getString(R.styleable.ConfirmButtonipetrpide_cbText)
                    cbTextColoripetrpide = getColor(
                        R.styleable.ConfirmButtonipetrpide_cbTextColor,
                        ContextCompat.getColor(context, R.color.white)
                    )
                    cbTextSizeipetrpide =
                        getDimension(
                            R.styleable.ConfirmButtonipetrpide_cbTextSize,
                            resources.getDimensionPixelSize(R.dimen.sp_14).toFloat()
                        )
                    cbBackgroundipetrpide = getResourceId(
                        R.styleable.ConfirmButtonipetrpide_cbBackground,
                        R.drawable.solid_blue500_r23
                    )
                    recycle()
                }
            }
        }

        context?.let {
            mConfirmRootView = inflate(it, R.layout.view_confirm_button, this)
            initSubViews()
        }
    }

    private fun initSubViews() {
        rl_ipetrpideconfirm_ipetrpidecontent =
            mConfirmRootView.findViewById(R.id.rl_confirm_content)
        tv_ipetrpideconfirm_ipetrpidebutton = mConfirmRootView.findViewById(R.id.tv_confirm_button)

        tv_ipetrpideconfirm_ipetrpidebutton.text = cbTextipetrpide?.safeipetrpide()
        tv_ipetrpideconfirm_ipetrpidebutton.setTextColor(cbTextColoripetrpide)
        tv_ipetrpideconfirm_ipetrpidebutton.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            cbTextSizeipetrpide
        )

        rl_ipetrpideconfirm_ipetrpidecontent.setBackgroundResource(cbBackgroundipetrpide)
    }

    fun recordipetrpideClickipetrpideRemark(
        remark: String? = System.currentTimeMillis().toString()
    ) {
        try {
            if (tag != null) {
                var gTag = tag as String
                if (gTag.isNotBlank()) {
                    gTag = "CLICK_$gTag"

                    var gRemark = remark
                    if (gRemark.isNullOrBlank()) {
                        gRemark = System.currentTimeMillis().toString()
                    }
//                    RiskDataHelpipetrpide.sycPointData(
//                        type = gTag,
//                        remark = gRemark
//                    )
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}